/*
 * Copyright 2014 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.superme.uaaservice.configure;


import com.superme.uaaservice.service.CustomUserDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.autoconfigure.ManagementServerProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Order(ManagementServerProperties.ACCESS_OVERRIDE_ORDER)
@Configuration
@EnableWebSecurity
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {

	@Autowired
	private CustomUserDetailsService userDetailsService;

	/*
	@Override
	protected void configure(AuthenticationManagerBuilder auth) throws Exception {
		auth.userDetailsService(userDetailsService);

		//可采用内存管理
		//auth.inMemoryAuthentication()
        //            .withUser("user").password("password").roles("USER")
        //            .and()
        //            .withUser("admin").password("admin").roles("ADMIN");

	}
	*/

	@Autowired
	public void globalUserDetails(final AuthenticationManagerBuilder auth) throws Exception {
		// @formatter:off
		//已经用数据库实现
		auth.userDetailsService(userDetailsService);

	}// @formatter:on


	@Override
	@Bean
	public AuthenticationManager authenticationManagerBean() throws Exception {
		return  super.authenticationManagerBean();
	}



	@Override
	protected void configure(HttpSecurity http) throws Exception {
/*
//这个可以从自定义页面进入
		http.formLogin().loginPage("/login").permitAll().and().authorizeRequests()
				.anyRequest().authenticated();
*/


		http.authorizeRequests().antMatchers("/login").permitAll().and()
			.formLogin().permitAll().and().authorizeRequests().anyRequest().authenticated();
/*
		//采用默认的登录界面
		http.formLogin().permitAll().and().authorizeRequests()
				.anyRequest().authenticated();
*/
	}


}

